﻿using AutoMapper;
using MediatR;
using Traceability.APP.Applications.Commands.YangZhi;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.APP.Applications.CommandHandlers.YangZhi
{
    public class YangZhiAddCommandHandler : IRequestHandler<YangZhiAddCommand, APIResult<object>>
    {
        private readonly IBaseRepository<FarmModel> _farmRepository;
        private readonly IMapper _mapper;

        public YangZhiAddCommandHandler(IBaseRepository<FarmModel> farmRepository, IMapper mapper)
        {
            _farmRepository = farmRepository;
            _mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(YangZhiAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.AddSuccess;
            result.Message = APIEnum.AddSuccess.Desc();
            FarmModel farm = _mapper.Map<FarmModel>(request);
            await _farmRepository.Add(farm);
            return await Task.FromResult(result);
        }
    }
}
